home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / pluginy Firefox / 7661 / 7661.xpi / components / RILtextDownloader.js < prev    next >
Text File  |  2009-12-23  |  5KB  |  152 lines

  1. /*
  2.  
  3. License: This source code may not be used in other applications whether they
  4. be personal, commercial, free, or paid without written permission from Read It Later.
  5.  
  6.  
  7. /////////
  8. DEVELOPER API: readitlaterlist.com/api/
  9. /////////
  10.  
  11. If you would like to customize Read It Later or build an application that works with
  12. Read it Later take a look at the READ IT LATER OPEN API:
  13. http://readitlaterlist.com/api/
  14.  
  15. Suggestions for additions to Read It Later are VERY welcome.  A large number of user
  16. suggestions have been implemented.  Please let me know of any additional features you
  17. are seeking at: http://readitlaterlist.com/support/
  18.  
  19. Thanks
  20.  
  21. */
  22.  
  23. Components.utils.import("resource://gre/modules/XPCOMUtils.jsm");
  24.  
  25. function RILtextDownloader() {
  26.     this.type           = 1;           
  27. }
  28.  
  29. // class definition
  30. RILtextDownloader.prototype = {
  31.  
  32.   // properties required for XPCOM registration:
  33.   classDescription: "Read It Later Text Downloader Javascript XPCOM Component",
  34.   classID:          Components.ID("{378c38a0-abaa-11de-8a39-0800200c9a66}"),
  35.   contractID:       "@ril.ideashower.com/riltextdownloader;1",
  36.  
  37.   QueryInterface: XPCOMUtils.generateQI([Components.interfaces.nsIRILtextDownloader]),
  38.  
  39.   //////////////////////////////////////////    
  40.     
  41.     init : function(itemId, url, callback)
  42.     {                
  43.         this.itemId = itemId;
  44.         this.url = url;
  45.         this.callback = callback ? callback : 'textFinished';
  46.         
  47.         this.APP    = Components.classes['@ril.ideashower.com/rildelegate;1'].getService().wrappedJSObject;        
  48.         this.ASSETS = Components.classes['@ril.ideashower.com/rilassetmanager;1'].createInstance(Components.interfaces.nsIRILassetManager);
  49.     this.ASSETS.init();
  50.         this.JSON   = Components.classes["@mozilla.org/dom/json;1"].createInstance(Components.interfaces.nsIJSON);
  51.         this.markupPath = this.ASSETS.folderPathForItemId( itemId ) + 'text.html';
  52.     
  53.     this.requestId = itemId + '-' + url + '-' + Math.random();
  54.     return this.requestId;
  55.     },
  56.     
  57.     
  58.      // Run on the main thread because of ajax request
  59.     start : function(threadId)
  60.     {
  61.     try
  62.     {
  63.         this.threadId = threadId;
  64.         this.APP.SYNC.request( 'text', false, '&url='+this.url, this, 'textCallback', 'none');
  65.         return;
  66.     } catch(e){Components.utils.reportError(e);}
  67.     
  68.     //else
  69.     this.finish(false);
  70.     },
  71.     
  72.     // This is performed on the main thread
  73.     textCallback : function(request, success, response)
  74.     {
  75.     try {
  76.     
  77.         if (this.finished) return false;
  78.     
  79.     this.title = request.header('X-Title');
  80.         
  81.     this.APP.d( 'status: ' + request.status );  
  82.     this.APP.d( 'X-Error: ' + request.error );  
  83.         this.APP.d( 'X-Title: ' + request.header('X-Title') );
  84.     
  85.     if (!request.success)
  86.     {
  87.         this.finish( false, request.status );
  88.         this.error = request.error;        
  89.     }
  90.     else
  91.     {
  92.     
  93.         // Update stylesheet
  94.         let markup = response;
  95.         
  96.         markup = markup.replace('<!--!ENDOFHEADSECTION-->', '<link type="text/css" rel="stylesheet" href="chrome://isreaditlater/content/text.css" /><script type="text/javascript" src="chrome://isreaditlater/content/text.js"></script>');
  97.         markup = markup.replace('<!--!TOPOFCTN-->',
  98.         '<div id="RIL_header">\
  99.             <div id="RIL_top">\
  100.             <cite>\
  101.                 <span>\
  102.                 <br /><br />Original: \
  103.                 </span>\
  104.                 <a href="'+this.url+'" target="_blank">'+this.url+'</a>\
  105.             </cite>\
  106.             <a class="i" id="RIL_settings"></a>\
  107.             </div>\
  108.             <div id="RIL_settings_wrapper"></div>\
  109.             <br />\
  110.             <h1>'+request.header('X-Title')+'</h1>\
  111.         </div>\
  112.         \
  113.         <ul id="RIL_nav">\
  114.             <li>Show:</li>\
  115.             <li id="nav_less"><a>Less</a></li>\
  116.             <li id="nav_more"><a>More</a></li>\
  117.         </ul>\
  118.         ');
  119.                 
  120.         // -- Save text to file
  121.         
  122.         // Write file in thread
  123.             this.APP.OFFLINE.write( this.markupPath, markup, false, this, 'finish');
  124.         
  125.     }
  126.     
  127.     } catch(e) {Components.utils.reportError(e);}
  128.     },
  129.     
  130.     finish : function(success, statusCode)
  131.     {
  132.         this.finished = true;
  133.         this.success = success;
  134.         this.statusCode = statusCode ? statusCode : (success ? 1 : -1);
  135.         this.APP.OFFLINE[this.callback].call(this.APP.OFFLINE, this);
  136.     },
  137.     
  138.     cancel : function(soft)
  139.     {
  140.         //dump("\n -- cancelling.. ");
  141.         
  142.         this.finished = true;
  143.         
  144.     }    
  145.     
  146. };
  147.  
  148. var components = [RILtextDownloader];
  149. function NSGetModule(compMgr, fileSpec) {
  150.   return XPCOMUtils.generateModule(components);
  151. }
  152.